from flask import *
from flask_sqlalchemy import *
import os


class Config:
    DEBUG = True
    APP_PATH = os.path.dirname(__file__)
    SQLALCHEMY_DATABASE_URI = 'mysql://root:123@127.0.0.1:3306/study'
    SQLALCHEMY_TRACK_MODIFICATIONS = True


app = Flask(__name__)
app.config.from_object(Config)  # 先指定配置类
# 创建数据库链接
db = SQLAlchemy(app)


class User(db.Model):
    tablename = "user"
    # 模型类需要自定义主键
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), index=True)
    password = db.Column(db.String(100))


if __name__ == '__main__':
    db.create_all()
    user = User()
    user.username = "zyd"
    user.password = '123'
    # 添加对象到数据库
    db.session.add(user)
    # 最后需要提交
    db.session.commit()
